package
{
	import com.riaidea.utils.zip.ZipArchive;
	import com.riaidea.utils.zip.ZipEvent;
	import com.riaidea.utils.zip.ZipFile;
	
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.utils.Dictionary;
	
	import ricky.loader.MultipleLoader;
	import ricky.loader.Request;
	
	import rickyGame.model.ConfigUnitHelper;
	import rickyGame.model.UnitKey;
	import rickyGame.model.config.Ability;
	import rickyGame.model.config.AbilityData;
	import rickyGame.ui.loading.Loading;
	import rickyGame.model.User;
	
	/**
	 * game loader
	 */
	[SWF(backgroundColor="#000000")]
	public class Loader extends Sprite
	{
		public function Loader()
		{
			super();
			stage.scaleMode=StageScaleMode.NO_SCALE;
			stage.align=StageAlign.TOP_LEFT;	
			stage.frameRate=60;
			
			mulitLoader=new MultipleLoader();
			loading=new Loading();
			loading.x=(this.stage.stageWidth-800)/2
			loading.y=this.stage.stageHeight-200;
			this.addChild(loading);
			var url:String;
			var obj:Object=loaderInfo.parameters;
			for(var v:String in obj)
			{
				url=v;
			}
			

			User.userName="a123";

			var req5:Request=new Request("Game.swf?"+url,onMainComplete);//main 
			var req1:Request=new Request("config\\language\\lan.zip?"+url,onLanComplete);
			var req2:Request=new Request("config\\ability\\ability.zip?"+url,onAbiliComplete,3);
			var req3:Request=new Request("config\\unit\\unit.zip?"+url,onUnitComplete,2);
			mulitLoader.loadReq([req1,req5,req2,req3],onComplete,loading.onProgress);
		}
		
		private function onAbiliComplete(e:Request):void
		{
			trace("Ability 2");
			
			var zip:ZipArchive=new ZipArchive();
			zip.open(e.data);
			//初始化技能数据
			var key1:String=UnitKey.KeyAbilityData;
			var f:ZipFile=zip.getFileByName(key1);
			ConfigUnitHelper.init(key1,f.data.toString(),AbilityData,"abDataID");
			
			
			//初始化技能
			var key2:String=UnitKey.KeyAbility;
			ConfigUnitHelper.init(key2,f.data.toString(),Ability,"abilityID");
			
			var alldata:Dictionary=ConfigUnitHelper.getDicConfigByKey(key2);
			var abItem:Dictionary=ConfigUnitHelper.getDicConfigByKey(key1);
			var itemdata:Array,item:Object;
			for each(var ab:Object in alldata)
			{
				if(ab.data)
					itemdata=ab.data.split(","); 
				else
					itemdata=[];
				ab.data=[];
				for each(var abName:String in itemdata)
				{
					item=abItem[abName];
					if(item)
						ab.data.push(item);
				}
				
				JDebug.traceMe(ab,"技能")
			}
			
		}
		
		private function onUnitComplete(e:Request):void
		{
			JDebug.trace("unit 3");
			
			var zip:ZipArchive=new ZipArchive();
			zip.open(e.data);
			
			for each(var key:String in UnitKey.units)
			{
				var f:ZipFile=zip.getFileByName(key);
				ConfigUnitHelper.initUnit(key,f.data.toString());
			}
			
		}
		
		private function onLanComplete(e:Request):void
		{
			var zip:ZipArchive=new ZipArchive();
			zip.addEventListener(ZipEvent.ERROR,onError);
			zip.open(e.data);
			var f:ZipFile=zip.getFileByName("lan");
			CLocale.loadLanguage(f.data.toString());
		}
		
		private function onComplete(e:*):void
		{
			if(main)
			{
				Game(main).loading=loading;
				this.stage.addChild(main);
			}
		}
		
		private function onMainComplete(e:Request):void
		{
			main=e.data;
		}
		
		private function onError(e:ZipEvent):void
		{
			trace(e.message);
		}
		
		private var main:Sprite;
		private var mulitLoader:MultipleLoader;
		private var loading:Loading;
	}
}